package leetcode.n2021;

/**
 * 最长公共前缀
 * https://leetcode-cn.com/problems/longest-common-prefix/
 * @author Chaos
 * @date 2021/6/2
 */
public class LongestCommonPrefix {

    public static void main(String[] args) {
        LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
        System.out.println(longestCommonPrefix.test(new String[]{"flower","flow","flight"}));
    }


    public String test(String[] strs) {

        boolean flag = false;
        int size = 2500;
        String a = "";
        for (String str : strs) {
            if (size >= str.length()) {
                size = str.length();
                a = str;
            }
        }

        String b = "";
        for (int i = size; i > 0 ; i--) {
            for (String str : strs) {
                if (a.substring(0,i).equals(str.substring(0,i))) {
                    flag = true;
                } else {
                    flag = false;
                    break;
                }
                b = a.substring(0,i);
            }
            if (flag) {
                return b;
            }
        }

        return "";
    }
}
